package com.vitas.coin.manager;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;


import com.vitas.coin.data.bean.LocalMusicInfoBean;

import java.util.ArrayList;

public class LocalMusicManager {
    private static LocalMusicManager sInstance;
    private final ContentResolver mContentResolver;
    private final Context mContext;

    public static LocalMusicManager getInstance(Context context) {
        if (sInstance == null)
            sInstance = new LocalMusicManager(context);
        return sInstance;
    }

    private LocalMusicManager(Context context) {
        mContext = context.getApplicationContext();
        mContentResolver = context.getApplicationContext().getContentResolver();
    }

    public ArrayList<LocalMusicInfoBean> getAllMusic() {
        ArrayList<LocalMusicInfoBean> musicList = new ArrayList<>();
        String[] mediaColumns = new String[]{
                MediaStore.Audio.AudioColumns._ID,
                MediaStore.Audio.AudioColumns.DATA,
                MediaStore.Audio.AudioColumns.TITLE,
                MediaStore.Audio.AudioColumns.ARTIST,
                MediaStore.Audio.AudioColumns.DURATION
        };
        Cursor cursor = mContentResolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                mediaColumns, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                LocalMusicInfoBean info = new LocalMusicInfoBean();
                info.setDuration(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
                if (info.getDuration() <= 0) {
                    continue;
                }
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                if (!path.endsWith(".mp3")) {// 仅支持mp3或wva
                    continue;
                }
                info.setPath(path);
                info.setSongName(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                info.setSingerName(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                info.setFormatDuration(duration(info.getDuration()/1000));
                musicList.add(info);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return musicList;
    }


    /**
     *
     * @param duration 秒
     * @return
     */
    public String duration(long duration) {
        long h = duration / 3600;
        long m = (duration - h * 3600) / 60;
        long s = duration - (h * 3600 + m * 60);

        String durationValue;

        if (h == 0) {
            durationValue = asTwoDigit(m) + ":" + asTwoDigit(s);
        } else {
            durationValue = asTwoDigit(h) + ":" + asTwoDigit(m) + ":" + asTwoDigit(s);
        }
        return durationValue;
    }

    public String asTwoDigit(long digit) {
        String value = "";

        if (digit < 10) {
            value = "0";
        }

        value += String.valueOf(digit);
        return value;
    }














}
